package com.baidu.unlockdemo.customview

import android.animation.ValueAnimator
import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.animation.AccelerateInterpolator
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import android.widget.RelativeLayout
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.animation.addListener
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.BindingAdapter
import com.baidu.unlockdemo.R
import com.baidu.unlockdemo.databinding.ActivityCustomBinding

class CustomActivity : AppCompatActivity() {
    private lateinit var binding: ActivityCustomBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityCustomBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        //addTextViewToConstraintLayout()
        //addTextViewToFrameLayout()
        //addTextViewToRelativeLayout()

        //点击屏幕触发动画
        binding.main.setOnClickListener {
            //属性动画
            //动画改变的都是对应的属性，或者 产生一系列的值
            val va = ValueAnimator.ofFloat(0f,1f)
            //监听产生的每一个值
            va.addUpdateListener {
                //获取当前得到的一个值
                val num = it.animatedValue as Float
                //通知视图刷新界面
                binding.animationBtn.setProgress(num)
            }
            va.duration = 5000
            va.interpolator = LinearInterpolator()
            va.start()
            va.addListener(onStart = {}, onEnd = {})

        }
    }

    private fun addTextViewToRelativeLayout() {
        val textView = AppCompatTextView(this)
        textView.text = "Android Dev"
        textView.setTextColor(Color.MAGENTA)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,50f)
        textView.setBackgroundColor(Color.GREEN)

        val lp = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            dp2px(100))
//        lp.addRule(RelativeLayout.ABOVE, R.id.title)
//        lp.addRule(RelativeLayout.ALIGN_START, R.id.title)
//        lp.addRule(RelativeLayout.ALIGN_END, R.id.title)

        binding.root.addView(textView,lp)
    }

    private fun addTextViewToFrameLayout() {
        val textView = AppCompatTextView(this)
        textView.text = "Android Dev"
        textView.setTextColor(Color.MAGENTA)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,50f)
        textView.setBackgroundColor(Color.GREEN)
        textView.gravity = Gravity.CENTER

        val lp = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT,
            dp2px(200))
        lp.gravity = Gravity.CENTER

        binding.root.addView(textView,lp)
    }

    /**
     * 手动添加一个TextView到ConstraintLayout中
     */
    private fun addTextViewToConstraintLayout(){
        //1. 创建TextView
        val textView = AppCompatTextView(this)
        //2.配置这个控件在容器中的位置和尺寸 ConstraintLayout
        //  所有的容器ViewGroup类 都有一个内部的类 LayoutParams
        //  用来管理子控件如何布局
        val lp = ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.WRAP_CONTENT,
            dp2px(50),
        )
        lp.leftToLeft = R.id.main
        lp.rightToRight = R.id.main
        lp.topToTop = R.id.main
        lp.bottomToBottom = R.id.main

        //3. 配置内容
        textView.text = "Android Dev"
        textView.setTextColor(Color.MAGENTA)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,50f)

        //4. 添加到容器中
        binding.root.addView(textView, lp)

    }

    fun dp2px(dp: Int): Int = (dp * resources.displayMetrics.density + 0.5f).toInt()
}









